package com.company.manage.system.utils;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Created by HuangYong on 2016/1/23.
 * desc:
 */
public class AjaxFilter implements Filter {
    private static final String TAG = "AjaxFilter";

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain chain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        HttpServletResponse response = (HttpServletResponse) servletResponse;

        String ajaxSubmit = request.getHeader("X-Requested-With");
        if(ajaxSubmit!=null && ajaxSubmit.equals("XMLHttpRequest")){
            if(request.getSession(false)==null){
                response.setHeader("sessionstatus","timeout");
                response.getWriter().print("sessionstatus");
                return;
            }
        }
        chain.doFilter(servletRequest,servletResponse);
    }

    @Override
    public void destroy() {

    }
}